package com.cw.jeeyt.controller.system;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemAboutUsBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemAboutUsService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 关于我们控制层
 *
 * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/systemAboutUs")
@Slf4j
public class SystemAboutUsController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(SystemAboutUsController.class);
    @Autowired
    private SystemAboutUsService systemAboutUsService;

    /**
     * 跳转到关于我们列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {
        ModelAndView mv = new ModelAndView("system/systemAboutUs_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemAboutUs")
    public ResultData listSystemAboutUs(@RequestBody DataTable dataTable) throws Exception {

        SystemAboutUsBo systemAboutUsBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemAboutUsBo.class);
        if (systemAboutUsBo == null) {
            systemAboutUsBo = new SystemAboutUsBo();
        }
        PageInfo<SystemAboutUsBo> pageInfo = systemAboutUsService.queryByPage(systemAboutUsBo, dataTable.getStart(),
            dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到关于我们表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(Long id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("system/systemAboutUs_form");
        SystemAboutUsBo systemAboutUsBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemAboutUsBo = new SystemAboutUsBo();
        } else {
            systemAboutUsBo = systemAboutUsService.getSystemAboutUsById(id);
        }
        mv.addObject("systemAboutUs", systemAboutUsBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 根据操作类型保存关于我们数据
     *
     * @param systemAboutUsBo
     * @param operation add 新增  edit 修改
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemAboutUs", method = RequestMethod.POST)
    public JsonResultDto saveSystemAboutUs(SystemAboutUsBo systemAboutUsBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemAboutUsService.insert(systemAboutUsBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemAboutUsService.update(systemAboutUsBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
            e.printStackTrace();
            logger.error("保存关于我们失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除关于我们
     *
     * @param systemAboutUsBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(SystemAboutUsBo systemAboutUsBo) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemAboutUsService.delete(systemAboutUsBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除关于我们失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
